﻿using MetroDataQuery.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MetroDataQuery.Utility
{
    public class ChangeLogQuery
    {
        /// <summary>
        /// 新增点号更改记录,覆盖已有的当天记录;重算该时间之后的所有累积数据.
        /// </summary>
        /// <param name="prjId">工程ID</param>
        /// <param name="ptName">点号</param>
        /// <param name="changeValue">更改的观测值增量,单位是米.例如:原来为1.234,现在为1.235,则增量为0.001</param>
        /// <param name="dateStr">时间</param>
        public static void AddChangeLog(int prjId, string ptName, double changeValue, string dateStr)
        {
            using (var db = new MonitorProjectsEntities())
            {
                var time = DateTime.Parse(dateStr);
                var existed = db.MonitorPointChangeLog.SingleOrDefault(p => p.PrjId == prjId &&
                    p.PointName == ptName && p.DateTime == time);
                if (existed != null)
                {
                    var oldV = existed.ChangeValue;
                    existed.ChangeValue = changeValue;
                    changeValue = changeValue - oldV;
                }
                else
                {
                    var changeLog = new MonitorPointChangeLog()
                    {
                        ChangeValue = changeValue,
                        PrjId = prjId,
                        DateTime = time,
                        PointName = ptName
                    };
                    db.MonitorPointChangeLog.Add(changeLog);
                }
                db.SaveChanges();
                changeValue = Math.Round(changeValue * 1000, 2);
                ChangeLogQuery.ApplyChange(prjId, ptName, changeValue, dateStr);
            }
        }

        /// <summary>
        /// 移除已有的更改记录,会导致之后的数据重算
        /// </summary>
        /// <param name="prjId">工程ID</param>
        /// <param name="ptName">点号</param>
        /// <param name="dateStr">时间</param>
        public static void RemoveChangeLog(int prjId, string ptName, string dateStr)
        {
            using (var db = new MonitorProjectsEntities())
            {
                var time = DateTime.Parse(dateStr);
                var existed = db.MonitorPointChangeLog.SingleOrDefault(p => p.PrjId == prjId &&
                    p.PointName == ptName && p.DateTime == time);
                if (existed != null)
                {
                    db.MonitorPointChangeLog.Remove(existed);
                    db.SaveChanges();
                    var changeValue = -existed.ChangeValue * 1000;
                    ChangeLogQuery.ApplyChange(prjId, ptName, changeValue, dateStr);
                }
            }

           

        }




        /// <summary>
        /// 应用更改,重算时间以后的所有累积数据
        /// </summary>
        /// <param name="prjId">工程id</param>
        /// <param name="ptName">点名</param>
        /// <param name="value">更改数值,以mm为单位</param>
        /// <param name="dateStr">时间</param>
        private static void ApplyChange(int prjId, string ptName, double value, string dateStr)
        {
            var dictInfo = PointsDictionaryQuery.GetDictPointByName(prjId, ptName);
            var resultTable = MetroDataBaseUtility.GetTableNameByMonitorType(prjId, dictInfo.MonitorType, true);

            using (var db = new MonitorProjectsEntities())
            {
                var sql = String.Format(@"update {0} set [Value] = Round([Value] + 
                                {1},2) where PrjId=@p0 and Name=@p1 and DateTime>=@p2", resultTable, value);
                db.Database.ExecuteSqlCommand(sql, prjId, ptName, dateStr);
            }

        }
    }
}
